package com.ezhuan.common.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSONObject;

/**
 * 发送短信的工具类
 * @author lenovo
 *
 */
public class SmsUtil
{
	private String HOST = "app.cloopen.com";
	private String ACCOUNT_SID = "aaf98f8952b6f5730152e2e690c8292b";
	private String AUTH_TOKEN = "98c203c1d345404fa311381828b6a3f8";
	private String APPID = "8a216da854dc9dc60154dce0c6d200d8";
	
	/**
	 * 发送短信
	 * @param checknum 如果值为test,表示假数据
	 * @return {"code":"0","info":"","data":{"thirdSn":"","createTime":""}}
	 */
	public JSONObject sendSms(String phone,String checknum,String yxtime)
	{
		JSONObject resultJSON = new JSONObject() ; 
		if("test".equalsIgnoreCase(yxtime))
		{
			/* 假数据 */
			resultJSON = testSuccedData();
			return resultJSON ; 
		}
		Map<String, Object> result = this.send_message(phone, checknum, yxtime);
		if (result != null) {
			if ("000000".equals(result.get("statusCode")))
			{
				HashMap data = (HashMap) result.get("data");
				HashMap data1 = (HashMap) data.get("templateSMS");
				Set<String> keySet = data1.keySet();
				
				JSONObject dataJSON = new JSONObject() ; 
				dataJSON.put("thirdSn", data1.get("smsMessageSid"));
				dataJSON.put("createTime", data1.get("dateCreated"));
				
				/* 取出短信返回的数据 */
				resultJSON.put("data", dataJSON);
				resultJSON.put("code", "0");
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			}
		}
		return resultJSON ;
	}

	/**
	 * 测试的假数据
	 * @param resultJSON
	 */
	private JSONObject testSuccedData()
	{
		JSONObject resultJSON = new JSONObject() ; 
		/* -- 假数据 -- */
		JSONObject dataJSON = new JSONObject();
		dataJSON.put("thirdSn", "00000");
		dataJSON.put("createTime", "----");
		resultJSON.put("data", dataJSON);
		resultJSON.put("code", "0");
		resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		return resultJSON ; 
	}
	
	/**
	 * 发送短信
	 * @param tel
	 * @param checknum
	 * @param yxtime
	 * @return 
	 * "000000".equals(result.get("statusCode")发送成功
	 */
	private HashMap send_message(String tel, String checknum, String yxtime)
	{
		HashMap result = null;
		try
		{
			CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
			restAPI.init(HOST, "8883");
			// 初始化服务器地址和端口，沙盒环境配置成sandboxapp.cloopen.com，生产环境配置成app.cloopen.com，端口都是8883.
			restAPI.setAccount(ACCOUNT_SID, AUTH_TOKEN);
			// 初始化主账号名称和主账号令牌，登陆云通讯网站后，可在"控制台-应用"中看到开发者主账号ACCOUNT SID和
			// 主账号令牌AUTH TOKEN。
			restAPI.setAppId(APPID);
			// 初始化应用ID，如果是在沙盒环境开发，请配置"控制台-应用-测试DEMO"中的APPID。
			// 如切换到生产环境，请使用自己创建应用的APPID

			String[] s = new String[2];
			s[0] = checknum;
			s[1] = yxtime;
			result = restAPI.sendTemplateSMS(tel, "88376", s);
			ConstatFinalUtil.LOGGER.info("SDKTestGetSubAccounts result:{}" , result);
			/*
			 * 
			 * if("000000".equals(result.get("statusCode"))){
			 * //正常返回输出data包体信息（map） HashMap data = (HashMap)
			 * result.get("data"); Set keySet = data.keySet(); for(Object
			 * key:keySet){ Object object = data.get(key);
			 * System.out.println(key +" = "+object); } }else{ //异常返回输出错误码和错误信息
			 * System.out.println("错误码=" + result.get("statusCode")
			 * +" 错误信息= "+result.get("statusMsg")); }
			 */
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("发送短信失败了,phone:{},序列号:{}",tel,checknum,e);
		}
		return result;
	}
}
